#include "LocalScene.h"
#include "Utils/LogUtil.h"
#include "LocalEntity.h"
#include "Utils/StringUtil.h"
#include "Game/Component/Model.h"
#include "Render/RenderState.h"
#include "Render/Imgui/imgui.h"
#include "Math/Random.h"


void LocalScene::OnInit()
{
	mCamera.SetPosition(glm::vec3(0.f, 0.04f, 7.f));
	mCamera.LookAt(glm::vec3(0.f, 0.04f, 7.f), glm::vec3(0.f, 0.04f, 0.f));
	mCamera.UpdateViewMatrix();

	mCoordinateProcess = GetProcess<CoordinateProcess>("CoordinateProcess");

	GeometryGenerator geometryGenerator;

	// GeometryGenerator::MeshData lightData = geometryGenerator.CreatePanel(1.f, 1.f, glm::vec3(0.f, -1.f, 0.f));
	GeometryGenerator::MeshData lightData = geometryGenerator.CreatePanel(1.f, 1.f, glm::vec3(0.f, -1.f, 0.f));

	LocalEntity* lightEntity = CreateEntity<LocalEntity>("light");
	lightEntity->AddComponent<Model>(lightData);
	lightEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 1.9f, 0.f));
	lightEntity->GetTransform().UpdateMatrices();
	lightEntity->SetColor(glm::vec4(1.f));
	
	lightEntity->mMaterial.emissive = glm::vec3(10.f);
	lightEntity->mMaterial.baseColor = glm::vec3(1.f);


	GeometryGenerator::MeshData leftPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(-1.f, 0.f, 0.f));

	LocalEntity* leftPanelEntity = CreateEntity<LocalEntity>("leftPanel");
	leftPanelEntity->AddComponent<Model>(leftPanelData);
	leftPanelEntity->GetTransform().SetLocalPosition(glm::vec3(2.f, 0.f, 0.f));
	leftPanelEntity->GetTransform().UpdateMatrices();
	leftPanelEntity->SetColor(glm::vec4(0.65f, 0.05f, 0.05f, 1.f));
	
	leftPanelEntity->mMaterial.baseColor = glm::vec3(0.65f, 0.05f, 0.05f);


	GeometryGenerator::MeshData rightPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(1.f, 0.f, 0.f));

	LocalEntity* rightPanelEntity = CreateEntity<LocalEntity>("rightPanel");
	rightPanelEntity->AddComponent<Model>(rightPanelData);
	rightPanelEntity->GetTransform().SetLocalPosition(glm::vec3(-2.f, 0.f, 0.f));
	rightPanelEntity->GetTransform().UpdateMatrices();
	rightPanelEntity->SetColor(glm::vec4(0.12f, 0.45f, 0.15f, 1.f));
	
	rightPanelEntity->mMaterial.baseColor = glm::vec3(0.12f, 0.45f, 0.15f);


	GeometryGenerator::MeshData topPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(0.f, -1.f, 0.f));

	LocalEntity* topPanelEntity = CreateEntity<LocalEntity>("topPanel");
	topPanelEntity->AddComponent<Model>(topPanelData);
	topPanelEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 2.f, 0.f));
	topPanelEntity->GetTransform().UpdateMatrices();
	topPanelEntity->SetColor(glm::vec4(0.72f, 0.84f, 0.96f, 1.f));
	
	topPanelEntity->mMaterial.baseColor = glm::vec3(0.72f, 0.84f, 0.96f);


	GeometryGenerator::MeshData bottomPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(0.f, 1.f, 0.f));

	LocalEntity* bottomPanelEntity = CreateEntity<LocalEntity>("bottomPanel");
	bottomPanelEntity->AddComponent<Model>(bottomPanelData);
	bottomPanelEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, -2.f, 0.f));
	bottomPanelEntity->GetTransform().UpdateMatrices();
	bottomPanelEntity->SetColor(glm::vec4(0.90f, 0.72f, 0.88f, 1.f));

	bottomPanelEntity->mMaterial.baseColor = glm::vec3(0.90f, 0.72f, 0.88f);


	GeometryGenerator::MeshData backPanelData = geometryGenerator.CreatePanel(4.f, 4.f, glm::vec3(0.f, 0.f, 1.f));

	LocalEntity* backPanelEntity = CreateEntity<LocalEntity>("backPanel");
	backPanelEntity->AddComponent<Model>(backPanelData);
	backPanelEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 0.f, -2.f));
	backPanelEntity->GetTransform().UpdateMatrices();
	backPanelEntity->SetColor(glm::vec4(0.9f, 0.9f, 0.9f, 1.f));
	
	backPanelEntity->mMaterial.baseColor = glm::vec3(0.9f, 0.9f, 0.9f);

#if 1

	GeometryGenerator::MeshData sphereData = geometryGenerator.CreateSphere(0.8f, 20, 20);

	LocalEntity* reflectEntity = CreateEntity<LocalEntity>("reflect");
	reflectEntity->AddComponent<Model>(sphereData);
	reflectEntity->GetTransform().SetLocalPosition(glm::vec3(-0.6f, 0.8f, -0.5f));
	reflectEntity->GetTransform().UpdateMatrices();
	reflectEntity->SetColor(glm::vec4(1.f, 0.f, 0.f, 1.f));
	
	reflectEntity->mMaterial.baseColor = glm::vec3(1.f, 0.f, 0.f);


	LocalEntity* samplerEntity = CreateEntity<LocalEntity>("sampler");
	samplerEntity->AddComponent<Model>(sphereData);
	samplerEntity->GetTransform().SetLocalPosition(glm::vec3(-1.f, -1.2f, 0.7f));
	samplerEntity->GetTransform().UpdateMatrices();
	samplerEntity->SetColor(glm::vec4(0.f, 1.f, 0.f, 1.f));

	samplerEntity->mMaterial.baseColor = glm::vec3(0.f, 1.f, 0.f);


	GeometryGenerator::MeshData cubeData = geometryGenerator.CreateCube(1.2f, 2.2f, 1.2f, 1);

	LocalEntity* cubeEntity = CreateEntity<LocalEntity>("cube");
	cubeEntity->AddComponent<Model>(cubeData);
	cubeEntity->GetTransform().SetLocalPosition(glm::vec3(0.8f, -0.9f, -0.4f));
	cubeEntity->GetTransform().SetLocalOrientation(glm::quat(glm::vec3(0.f, glm::radians(60.f), 0.f)));
	cubeEntity->GetTransform().UpdateMatrices();
	cubeEntity->SetColor(glm::vec4(0.f, 0.f, 1.f, 1.f));
	
	cubeEntity->mMaterial.baseColor = glm::vec3(0.5f, 0.5f, 0.1f);

#else

	LocalEntity* dragonEntity = CreateEntity<LocalEntity>();
	// dragonEntity->AddComponent<Model>("Dragon.obj");
	dragonEntity->AddComponent<Model>("Bunny.obj");
	dragonEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, -2.f, 0.f));
	dragonEntity->GetTransform().SetLocalScale(glm::vec3(20.f));
	dragonEntity->GetTransform().UpdateMatrices();
	dragonEntity->SetColor(glm::vec4(1.f, 0.5f, 0.1f, 1.f));

	dragonEntity->mMaterial.baseColor = glm::vec3(1.f, 0.5f, 0.1f);

#endif
}

void LocalScene::OnExit()
{

}

void LocalScene::OnTick(float deltaTime)
{

}

void LocalScene::OnImgui()
{
	GameScene::OnImgui();

#if 0

	ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver);

	ImGui::Begin("MapOption");



	ImGui::End();

#endif
}
